﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace QuanLySuKien
{
    public partial class QuanLyKV : System.Web.UI.Page
    {
        string strCon = "Data Source=192.168.1.100;Initial Catalog=quanlysukien;User ID=sa;Password=123456";
        DataTable table = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        void LoadData()
        {
            try
            {
                SqlDataAdapter adapter = new SqlDataAdapter("Select * from KhuVuc", strCon);
                adapter.Fill(table);
                GridView1.DataSource = table;
                GridView1.DataBind();
            }
            catch (Exception ex)
            {
                StatusLabel.Text = ex.Message;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            bool success = false;
            string MaKhuVuc = txtMaKV.Text.ToString();
            string TenKhuVuc = txtTenKV.Text.ToString();
            string MoTa = txtMoTa.Text.ToString();

            try
            {
                string sql = "INSERT INTO KhuVuc (MaKV, TenKhuVuc, Mota) " + "VALUES (@prmMaKhuVuc, @prmTenKhuVuc, @prmMota)";
                using (SqlCommand command = new SqlCommand(sql, ConnectionManager.GetConnection()))
                {
                    command.Parameters.Add("@prmTenKhuVuc", SqlDbType.NVarChar, 100).Value = TenKhuVuc;
                    command.Parameters.Add("@prmMota", SqlDbType.NVarChar, 100).Value = MoTa;
                    command.Parameters.Add("@prmMaKhuVuc", SqlDbType.VarChar, 10).Value = MaKhuVuc;
                    int rowAffected = command.ExecuteNonQuery();
                    success = (rowAffected == 1);

                }
                if (success)
                {
                    StatusLabel.Text = "Khu Vực đã được cập nhật";
                    txtMaKV.Text = "";
                    txtTenKV.Text = "";
                    txtMoTa.Text = "";
                    LoadData();
                }
                else
                {
                    StatusLabel.Text = "Khu Vực chưa thể cập nhật";
                }
            }
            catch (SqlException ex)
            {
                StatusLabel.Text = ex.Message;
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            bool success = false;
            string MaKhuVuc = txtMaKV.Text.ToString();
            string MoTa = txtMoTa.Text.ToString();
            string TenKhuVuc = txtTenKV.Text.ToString();
            try
            {
                string sql = "UPDATE KhuVuc SET TenKhuVuc=@prmTenKhuVuc, Mota=@prmMota WHERE MaKV=@prmMaKhuVuc";
                using (SqlCommand command = new SqlCommand(sql, ConnectionManager.GetConnection()))
                {
                    command.Parameters.Add("@prmMaKhuVuc", SqlDbType.VarChar,10).Value = MaKhuVuc;
                    command.Parameters.Add("@prmMota", SqlDbType.NVarChar, 100).Value = MoTa;
                    command.Parameters.Add("@prmTenKhuVuc", SqlDbType.NVarChar, 100).Value = TenKhuVuc;
                    int rowAffected = command.ExecuteNonQuery();
                    success = (rowAffected == 1);

                }
                if (success)
                {
                    StatusLabel.Text = "Khu Vực đã cập nhật thành công";
                    txtMaKV.Text = "";
                    txtTenKV.Text = "";
                    txtMoTa.Text = "";
                    LoadData();
                }
                else
                {
                    StatusLabel.Text = "Khu Vực chưa thể cập nhật";
                }
            }
            catch (SqlException ex)
            {
                StatusLabel.Text = ex.Message;
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            bool success = false;
            string MaKhuVuc = txtMaKV.Text.ToString();

            try
            {
                string sql = "DELETE FROM KhuVuc WHERE MaKV=@prmMaKhuVuc";
                using (SqlCommand command = new SqlCommand(sql, ConnectionManager.GetConnection()))
                {
                    command.Parameters.Add("@prmMaKhuVuc", SqlDbType.VarChar,10).Value = MaKhuVuc;
                    
                    int rowAffected = command.ExecuteNonQuery();
                    success = (rowAffected == 1);

                }
                if (success)
                {
                    StatusLabel.Text = "Khu Vực đã xóa thành công";
                    txtMaKV.Text = "";
                    txtTenKV.Text = "";
                    txtMoTa.Text = "";
                    LoadData();
                }
                else
                {
                    StatusLabel.Text = "Khu Vực chưa thể xóa";
                }
            }
            catch (SqlException ex)
            {
                StatusLabel.Text = ex.Message;
            }
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            txtMaKV.Text = "";
            txtTenKV.Text = "";
            txtMoTa.Text = "";
        }
        internal sealed class ConnectionManager
        {
            public static SqlConnection GetConnection()
            {
                string connectionString = "Data Source=192.168.1.100;Initial Catalog=quanlysukien;User ID=sa;Password=123456";
                SqlConnection connection = new SqlConnection(connectionString);
                connection.Open();
                return connection;
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int row = GridView1.SelectedIndex;
            
            txtMaKV.Text = HttpUtility.HtmlDecode((string)GridView1.Rows[row].Cells[0].Text);
            txtTenKV.Text = HttpUtility.HtmlDecode((string)GridView1.Rows[row].Cells[1].Text);
            txtMoTa.Text = HttpUtility.HtmlDecode((string)GridView1.Rows[row].Cells[2].Text);
        }
    }
}